<?php

	class CartController extends AppController {
		var $name = 'Cart';
		var $uses = array('Product');
		
		var $productsCant = 0;
		
		var $publicAction = array('index', 'shop', 'add');

		function index() {
			$this->pageTitle = __('Carro de Compra', true);
			
			if (!empty($this->data)) {
				
			}
			
			$this->set('list', $this->Session->read('Cart.' . Configure::read('Shop.id') . '.Product'));
		}
		
		function delete($product_id = 0) {
			$product_id = unserialize(base64_decode($product_id));
			$this->Session->delete('Cart.' . Configure::read('Shop.id') . '.' . $product_id);
			$this->redirect($this->referer());
		}
		
		function add($product_id = 0) {
			$product_id = unserialize(base64_decode($product_id));
			
			$data = $this->Product->details($product_id);
			
			$this->Session->write('Cart.' . Configure::read('Shop.id') . '.Product.' . $product_id, $data);
			
			$this->Session->write('Cart.' . Configure::read('Shop.id') . '.Info.products', count($this->Session->read('Cart.' . Configure::read('Shop.id') . '.Product')));
			
			$price = Set::extract('{n}.Product.price', $this->Session->read('Cart.' . Configure::read('Shop.id') . '.Product'));
			$this->Session->write('Cart.' . Configure::read('Shop.id') . '.Info.total', array_sum($price));

			$this->redirect(array('controller' => 'cart', 'action' => 'index'));
		}
	}
